<?php

require_once dirname( __FILE__ ) . '/config.php';

if ( !empty( $_SERVER['REMOTE_ADDR'] ) && $_SERVER['REMOTE_ADDR'] != ADMIN_IP )
	die( 'The admin panel can only be accessed by the server admin' );

switch ( $_GET['action'] ) {
	case 'globalmsg':
		session_start();
		$message = preg_replace( '/\s\s+/', ' ', trim( stripslashes( $_GET['message'] ) ) );
		if ( !strlen( $message ) )
			die( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
	<title>Chat Admin</title>
</head>
<body>
	<h1>Chat Admin</h1>
	<p>The message was empty and could not be sent.</p>
</body>
</html>' );
		$message = urlencode( str_ireplace( $censored_words, 'BANANAS', $message ) );
		$message = "\n" . md5( $_SERVER['HTTP_USER_AGENT'] . '@' . $_SERVER['REMOTE_ADDR'] ) . urlencode( str_ireplace( $censored_words, 'BANANAS', $_SESSION['name'] ) )  . ':' . time() . ':Global:+' . $message;

		$channels = glob( dirname( __FILE__ ) . '/db/chat/*.txt' );

		foreach ( $channels as $channel ) {
			$sent = filemtime( $channel );
			$viewed = fileatime( $channel );
			$fh = fopen( $channel, 'a' );
			fwrite( $fh, $message );
			fclose( $fh );
			touch( $channel, $sent, $viewed );
		}

		die( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
	<title>Chat Admin</title>
</head>
<body>
	<h1>Chat Admin</h1>
	<p>Global message sent successfully to ' . number_format( count( $channels ) ) . ' channels.</p>
</body>
</html>' );
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
	<title>Chat Admin</title>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
	<script type="text/javascript" src="http://jqueryjs.googlecode.com/svn/trunk/plugins/color/jquery.color.js"></script>
	<script type="text/javascript">
//<![CDATA[
		$(function(){
			$('#globalmsg').hide().submit(function(){
				$(this).unbind('submit');
				$('body').load('admin.php?' + $(this).serialize());
				return false;
			}).find('h2').remove();

			$('<p/>').append($('<a href="javascript:void 0">Send global message</a>').click(function(){
				$('p').hide('normal', function(){
					$(this).remove();
				});
				$('#globalmsg').show('normal');
			})).append(' &bull; ').append($('<a href="javascript:void 0">Clean up expired chatrooms</a>').click(function(){
				$('p').hide('normal', function(){
					var wait = $('<h2/>').text('Please wait...').hide().appendTo('body').show('normal');
					$('<pre/>').hide().appendTo('body').load(
						'cleanup.php',
						'',
						function(){
							wait.hide('normal', function(){
								$(this).remove();
							});
							$(this).show('normal');
						}
					);
				});
			})).appendTo('body');
		});
//]]>
	</script>
	<style type="text/css">
		body {
			font-family: sans-serif;
		}
		h1, h2 {
			font-family: Impact, sans-serif;
			font-weight: normal;
		}
		#message {
			width: 100%;
		}
	</style>
</head>
<body>
	<h1>Chat Admin</h1>
	<form id="globalmsg" action="admin.php" method="get">
		<h2>Send global message</h2>
		<input id="message" name="message" type="text"/>
		<input type="hidden" id="action" name="action" value="globalmsg"/>
	</form>
</body>
</html>